package com.yuan.web.servlet;

import com.yuan.pojo.LinkMan;
import com.yuan.pojo.PageBean;
import com.yuan.service.LinkManService;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

@WebServlet("/linkman")
public class LinkManServlet extends HttpServlet {
    private LinkManService linkManService=new LinkManService();
    private Logger logger=LogManager.getLogger(LinkManService.class.getName());

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String action = request.getParameter("action");
        if(action.equals("findAll")){
            findAll(request,response);
        }
        if(action.equals("findOne")){
            findOne(request, response);
        }

        if(action.equals("add")){
            add(request,response);
        }
        if(action.equals("delete")){
            delete(request,response);
        }

        if(action.equals("update")){
            update(request,response);
        }
        if(action.equals("findByPage")){
            findByPage(request,response);
        }
    }

    private void findByPage(HttpServletRequest request, HttpServletResponse response) {
        try {
            Long currentPage = Long.valueOf((request.getParameter("currentPage")));
            Long pageSize =Long.valueOf(request.getParameter("pageSize"));
            PageBean Pagebean = linkManService.findByPage(currentPage, pageSize);
            request.setAttribute("pageBean",Pagebean);

            request.getRequestDispatcher("list_page.jsp").forward(request,response);
        } catch (Exception e) {
            logger.error(e);
        }

    }


    private void update(HttpServletRequest request, HttpServletResponse response) throws IOException {
        try {
            Map<String, String[]> parameterMap = request.getParameterMap();

            LinkMan linkMan = new LinkMan();

            BeanUtils.populate(linkMan,parameterMap);
            linkManService.update(linkMan);

            response.sendRedirect("linkman?action=findAll");
        } catch (Exception e) {
            logger.error(e);
            response.getWriter().write("修改失败");
        }


    }

    private void delete(HttpServletRequest request, HttpServletResponse response) {
        try {
            Integer id = Integer.valueOf(request.getParameter("id"));
            linkManService.delete(id);

            response.sendRedirect("linkman?action=findAll");

        } catch (Exception e) {
            logger.error(e);
        }
    }

    private void add(HttpServletRequest request, HttpServletResponse response) throws IOException {
        try {
            Map<String, String[]> parameterMap = request.getParameterMap();
            LinkMan linkMan = new LinkMan();
            BeanUtils.populate(linkMan,parameterMap);
            linkManService.add(linkMan);
            response.sendRedirect("linkman?action=findAll");
        }  catch (Exception e) {
            logger.error(e);
            response.getWriter().write("添加失败");
        }
    }

    private void findOne(HttpServletRequest request, HttpServletResponse response) throws IOException {
        try {
            Integer id = Integer.valueOf(request.getParameter("id"));
            LinkMan linkMan= linkManService.findOne(id);

            request.setAttribute("linkMan", linkMan);

            request.getRequestDispatcher("update.jsp").forward(request, response);
        } catch (Exception e) {
            logger.error(e);
            response.getWriter().write("回显数据失败");
        }
    }

    private void findAll(HttpServletRequest request, HttpServletResponse response) {
        try {
            List<LinkMan> linkManList = linkManService.findAll();
            request.setAttribute("list",linkManList);
            request.getRequestDispatcher("list.jsp").forward(request, response);
        } catch (Exception e) {
            logger.error(e);
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }
}